﻿using System;
using System.Collections.Generic;
using System.Text.RegularExpressions;
using System.Web;
using Hxsoft.AppV1.Module;

namespace Hxsoft.AppV1.WebUI
{
	public class ModuleSpecialModule : ISpecialModuleParser
	{
		public string BuildListUrl(string specialCode, string pageCode, string moduleCode, int? pageIndex)
		{
			return GenericUrlManager.BuildSpecialModuleListUrl(specialCode, pageCode, moduleCode, pageIndex);
		}

		public string BuildContentUrl(string specialCode, string pageCode, string moduleCode, int infoId, int? page)
		{
			return GenericUrlManager.BuildSpecialModuleContentUrl(specialCode, pageCode, moduleCode, infoId.ToString(), page);
		}

		public string BuildContentCommentUrl(string specialCode, string pageCode, string moduleCode, int infoId, int? page)
		{
			return GenericUrlManager.BuildSpecialModuleContentCommentUrl(specialCode, pageCode, moduleCode, infoId.ToString(), page);
		}

		public string ParsePart(SpecialTemplateConfiguration templateConfiguration, SpecialModule module, SpecialPageConfiguration configuration, Special specialInfo, SpecialPage pageInfo, ref SpecialPageInfo page, bool isPreview)
		{
			string content = "";
			if (configuration == null) return content;
			if (module == null || module.ModuleId != configuration.ModuleId) return content;

			SpecialArticlePageConfiguration con = configuration as SpecialArticlePageConfiguration;
			if (con == null) con = new SpecialArticlePageConfiguration();
			if (string.IsNullOrEmpty(con.ListTarget)) con.ListTarget = "_blank";
			if (string.IsNullOrEmpty(con.ItemTarget)) con.ItemTarget = "_blank";
			SpecialContentTemplate contentTemplate = templateConfiguration.GetBlockTemplate(module.ModuleType, con.ListType);
			if (contentTemplate == null) return content;

			SpecialArticleContentConfiguration moduleCon = module.ContentConfigurationObject as SpecialArticleContentConfiguration;
			string contentPageCode = moduleCon == null ? "" : moduleCon.ContentPage;

			content = contentTemplate.GetTemplate(con.Parameters);
			Regex reg = new Regex(@"<headerTemplate>((?:.|\n|\r)*?)</headerTemplate>", RegexOptions.IgnoreCase);
			if (con.ShowHeader == false)
				content = reg.Replace(content, "");
			else
				content = reg.Replace(content, "$1");

			Regex regMore = new Regex(@"<moreTemplate>((?:.|\n|\r)*?)</moreTemplate>", RegexOptions.IgnoreCase);
			bool showMore = con.ShowMore.HasValue ? con.ShowMore.Value : con.LoadCount.HasValue;
			if (showMore == false)
				content = regMore.Replace(content, "");
			else
				content = regMore.Replace(content, "$1");

			content = content.Replace("{ListTitle}", module.ModuleName);
			content = content.Replace("{ListUrl}", BuildListUrl(specialInfo.SpecialCode, contentPageCode, module.ModuleCode, null));
			content = content.Replace("{ListTarget}", con.ListTarget);
			content = content.Replace("{ListTargetAttribute}", string.IsNullOrEmpty(con.ListTarget) ? "" : @" target=""" + con.ListTarget + @"""");
			content = content.Replace("{CssClass}", string.IsNullOrEmpty(con.CssClass) ? "" : @" class=""" + con.CssClass + @"""");
			content = content.Replace("{AttachedClass}", string.IsNullOrEmpty(con.CssClass) ? "" : @" " + con.CssClass);
			content = content.Replace("{ImageWidthStyle}", con.ImageWidth.HasValue ? "width:" + con.ImageWidth.ToString() + "px;" : "");
			content = content.Replace("{ImageHeightStyle}", con.ImageHeight.HasValue ? "height:" + con.ImageHeight.ToString() + "px;" : "");
			content = content.Replace("{ModuleIndex}", page.ModuleCounter.ToString());

			List<Articles.ModuleArticleForClientSummaryList> list = null;

			if (isPreview)	// 读预览数据。
			{
				string json = templateConfiguration.GetBlockPreviewData(module.ModuleType, contentTemplate.ViewType);
				if (!string.IsNullOrEmpty(json)) list = Serialization.DeserializeJson<List<Articles.ModuleArticleForClientSummaryList>>(json);
			}

			if (list == null) list = Articles.GetSummaryList(null, int.Parse(module.RefModuleId), null, con.LoadCount);

			// 默认图片Url。
			string defaultImageUrl = string.IsNullOrEmpty(contentTemplate.DefaultImageUrl) ? templateConfiguration.DefaultImageUrl : contentTemplate.DefaultImageUrl;
			if (!string.IsNullOrEmpty(defaultImageUrl))
			{
				defaultImageUrl = defaultImageUrl.Replace(SpecialPages.TemplateConst_TemplateRootUrl, SpecialPages.TemplateRootUrl);
				defaultImageUrl = defaultImageUrl.Replace(SpecialPages.TemplateConst_TemplateFolder, pageInfo.TemplateRootUrl);
			}

			Regex reg2 = new Regex("<itemTemplate>((?:.|\n|\r)*?)</itemTemplate>", RegexOptions.IgnoreCase);
			Match match2 = reg2.Match(content);
			if (match2 != Match.Empty)
			{
				string items = "";
				string itemTemplate = match2.Groups[1].Value;
				string imageStyle = "";
				if (con.ImageWidth.HasValue || con.ImageHeight.HasValue) imageStyle = @" style=""" + (con.ImageWidth.HasValue ? "width:" + con.ImageWidth + "px;" : "") + (con.ImageHeight.HasValue ? "height:" + con.ImageHeight + "px;" : "") + @"""";
				foreach (var data in list)
				{
					string item = itemTemplate.Replace("{ItemUrl}", BuildContentUrl(specialInfo.SpecialCode, contentPageCode, module.ModuleCode, data.ArticleId, null));
					item = item.Replace("{ItemTarget}", con.ItemTarget);
					item = item.Replace("{ItemTargetAttribute}", string.IsNullOrEmpty(con.ItemTarget) ? "" : @" target=""" + con.ItemTarget + @"""");
					item = item.Replace("{ItemImage}", string.IsNullOrEmpty(data.ImageType) && string.IsNullOrEmpty(defaultImageUrl) ? "" : string.Format(@"<img src=""{0}"" alt=""{1}""{2} />", GenericUrlManager.GetInfoResourceThumbnailGeneratorUrl(data.ImageType, null, null, defaultImageUrl), data.Title, imageStyle));
					item = item.Replace("{ItemSourceTitle}", data.Title);
					item = item.Replace("{ItemTitle}", StringExtensions.CutString(data.Title, con.TitleCapacity, "..."));
					item = item.Replace("{ItemReleaseDate}", data.ReleaseDate.ToShortDateString());
					item = item.Replace("{ItemEncodeSourceTitle}", HttpUtility.HtmlEncode(data.Title));
					item = item.Replace("{ItemImageUrl}", GenericUrlManager.GetInfoResourceThumbnailGeneratorUrl(data.ImageType, null));
					item = item.Replace("{ItemSummary}", data.Summary);

					items += item;
				}

				content = reg2.Replace(content, items);
			}

			if (contentTemplate.HeadIncludeFiles != null)	// 头部包含文件
			{
				foreach (var file in contentTemplate.HeadIncludeFiles)
					page.RegisterHeadIncludeFile(file.Key, file.Value);
			}

			if (isPreview)		// 加载预览样式
			{
				page.TemplateCssUrls = templateConfiguration.TemplateCssUrls;
				if (page.TemplateCssUrls != null)
				{
					for (int i = 0; i < page.TemplateCssUrls.Length; i++)
					{
						var url = page.TemplateCssUrls[i];
						if (!string.IsNullOrEmpty(url))
							page.TemplateCssUrls[i] = url.Replace(SpecialPages.TemplateConst_TemplateRootUrl, SpecialPages.TemplateRootUrl);
					}
				}
			}

			// 替换其它模板变量
			content = GenericHelper.ReplaceSpecialTemplateParameters(content, contentTemplate.OtherParameters, con.Parameters);

			return content;
		}

		public string GetContentPageNavHtml(string specialCode, string pageCode, string moduleCode, int infoId, int? currentPage, List<string> pageContents)
		{
			if (pageContents == null || pageContents.Count <= 1) return "";
			string html = "";

			int page = currentPage.GetValueOrDefault(1);
			for (int i = 1; i <= pageContents.Count; i++)
			{
				if (i == page)
					html += @" <span class=""pagesCur"">" + i + @"</span> ";
				else
				{
					int? p = i;
					if (p == 1) p = null;
					string url = BuildContentUrl(specialCode, pageCode, moduleCode, infoId, p);
					html += @" <a href=""" + url + @""">" + i + "</a> ";
				}
			}

			if (page > 1)
			{
				int? p = page - 1;
				if (p == 1) p = null;
				string url = BuildContentUrl(specialCode, pageCode, moduleCode, infoId, p);

				html = @"<a href=""" + url + @""">上一页</a>" + html;
			}

			if (page < pageContents.Count)
			{
				string url = BuildContentUrl(specialCode, pageCode, moduleCode, infoId, page + 1);
				html += @"<a href=""" + url + @""">下一页</a>";
			}

			html = @"<div class=""pagination"">" + html + @"</div>";

			return html;
		}

		public static string GetCurrentPageContent(List<string> pageContents, int? currentPage)
		{
			if (pageContents == null || pageContents.Count <= 0) return "";
			int page = currentPage.GetValueOrDefault(1);
			if (page < 1) page = 1;
			if (page > pageContents.Count) page = pageContents.Count;
			return pageContents[page - 1];
		}

		public string ParseContent(SpecialTemplateConfiguration templateConfiguration, SpecialModule module, Special specialInfo, SpecialPage pageInfo, ref SpecialPageInfo page, bool isPreview)
		{
			string content = "";
			if (module == null) return content;

			SpecialArticleContentConfiguration con = module.ContentConfigurationObject as SpecialArticleContentConfiguration;
			if (con == null) con = new SpecialArticleContentConfiguration();
			if (string.IsNullOrEmpty(con.ListItemTarget)) con.ListItemTarget = "_blank";
			SpecialContentTemplate contentTemplate = templateConfiguration.GetContentTemplate(module.ModuleType, SpecialArticleContentViewType.Content.ToString(), con.ContentType);
			if (contentTemplate == null) return content;

			int infoId = 0;
			Article article = null;
			int? currentPage = null;

			if (isPreview)
			{
				string json = templateConfiguration.GetContentPreviewData(module.ModuleType, SpecialArticleContentViewType.Content.ToString(), contentTemplate.ViewType);
				if (!string.IsNullOrEmpty(json)) article = Serialization.DeserializeJson<Article>(json);
			}
			else
			{
				HttpContext context = HttpContext.Current;
				infoId = int.Parse(context.Request.QueryString[GenericUrlManager.Special_Query_ModuleInfoIdField]);
				currentPage = IntExtensions.ParseOrNull(context.Request.QueryString[GenericUrlManager.Special_Query_ModuleInfoContentPageField]);
			}

			if (article == null) article = Articles.Read(infoId);
			if (article == null) return content;

			string contentPageCode = con.ContentPage;
			string contentUrl = BuildContentUrl(specialInfo.SpecialCode, contentPageCode, module.ModuleCode, infoId, currentPage);

			// 内容分页
			List<string> pageContents = article.SplitContent();
			string pageContent = GetCurrentPageContent(pageContents, currentPage);
			pageContent += GetContentPageNavHtml(specialInfo.SpecialCode, contentPageCode, module.ModuleCode, infoId, currentPage, pageContents);

			if(!isPreview) GenericHelper.CheckLogin(contentUrl, () => ModuleArticles.GetReadRight(ModuleArticles.GetItem(int.Parse(module.RefModuleId), infoId)));

			content = contentTemplate.GetTemplate(con.ContentParameters);
			// 分离出评论模板
			Regex reg = new Regex(@"<commentTemplate>((?:.|\n|\r)*?)</commentTemplate>", RegexOptions.IgnoreCase);
			Match commentMatch = reg.Match(content);
			string commentTemplate = null;
			if (commentMatch != Match.Empty)
				commentTemplate = commentMatch.Groups[1].Value;

			content = content.Replace("{Title}", article.Title);
			content = content.Replace("{ViceTitle}", article.ViceTitle);
			content = content.Replace("{Source}", article.ArtSource);
			content = content.Replace("{ReadCount}", article.ReadCount.ToString());
			content = content.Replace("{ReleaseDate}", article.ReleaseDate.ToString());
			content = content.Replace("{Summary}", article.Summary);
			content = content.Replace("{Content}", pageContent);
			content = content.Replace("{CssClass}", string.IsNullOrEmpty(con.ContentCssClass) ? "" : @" class=""" + con.ContentCssClass + @"""");
			content = content.Replace("{AttachedClass}", string.IsNullOrEmpty(con.ContentCssClass) ? "" : @" " + con.ContentCssClass);
			content = content.Replace("{ModuleIndex}", page.ModuleCounter.ToString());

			if (contentTemplate.HeadIncludeFiles != null)	// 头部包含文件
			{
				foreach (var file in contentTemplate.HeadIncludeFiles)
					page.RegisterHeadIncludeFile(file.Key, file.Value);
			}
			
			if (isPreview)		// 加载预览样式
			{
				page.TemplateCssUrls = templateConfiguration.TemplateCssUrls;
				if (page.TemplateCssUrls != null)
				{
					for (int i = 0; i < page.TemplateCssUrls.Length; i++)
					{
						var url = page.TemplateCssUrls[i];
						if (!string.IsNullOrEmpty(url))
							page.TemplateCssUrls[i] = url.Replace(SpecialPages.TemplateConst_TemplateRootUrl, SpecialPages.TemplateRootUrl);
					}
				}
			}

			// 替换其它模板变量
			content = GenericHelper.ReplaceSpecialTemplateParameters(content, contentTemplate.OtherParameters, con.ContentParameters);

			// 当前位置
			page.Locations.Add(new NavigationItem() { Text = module.ModuleName, Url = BuildListUrl(specialInfo.SpecialCode, contentPageCode, module.ModuleCode, null) });
			page.Locations.Add(new NavigationItem() { Text = Res.Content, Url = contentUrl });
			page.Title = article.Title + Hxsoft.AppV1.Page.TitleSuffix;	// 更新页面标题
			page.MetaKeywords = article.Title + (string.IsNullOrEmpty(article.Keywords) ? "" : "," + article.Keywords);
			page.MetaDescription = article.Summary;

			// 评论
			if (!string.IsNullOrEmpty(commentTemplate) && !InfoCommentStates.IsClosedComment(Articles.INFO_TYPE, infoId.ToString()))
			{
				string commentContent = CommentSpecialModule.ParseCommentForm(page, commentTemplate, Articles.INFO_TYPE, article.ArticleId.ToString(), page.ModuleCounter, false);

				List<Comments.CommentForClientList> commentList = null;
				if (isPreview)
				{
					string json = templateConfiguration.GetBlockPreviewData("Comment", "");
					if (!string.IsNullOrEmpty(json)) commentList = Serialization.DeserializeJson<List<Comments.CommentForClientList>>(json);
				}
				else
					commentList = Comments.GetList(null, Articles.INFO_TYPE, article.ArticleId.ToString(), 5);

				commentContent = CommentSpecialModule.ParseCommentList(page, commentContent, commentList, true, true, 5, "相关评论", BuildContentCommentUrl(specialInfo.SpecialCode, contentPageCode, module.ModuleCode, infoId, null), null, null);
				content = reg.Replace(content, commentContent);
			}
			else
				content = reg.Replace(content, "");

			return content;
		}

		public string ParseList(SpecialTemplateConfiguration templateConfiguration, SpecialModule module, Special specialInfo, SpecialPage pageInfo, ref SpecialPageInfo page, bool isPreview)
		{
			string content = "";
			if (module == null) return content;

			SpecialArticleContentConfiguration con = module.ContentConfigurationObject as SpecialArticleContentConfiguration;
			if (con == null) con = new SpecialArticleContentConfiguration();
			if (string.IsNullOrEmpty(con.ListItemTarget)) con.ListItemTarget = "_blank";
			SpecialContentTemplate contentTemplate = templateConfiguration.GetContentTemplate(module.ModuleType, SpecialArticleContentViewType.List.ToString(), con.ListType);
			if (contentTemplate == null) return content;

			string contentPageCode = con.ContentPage;
			int currentPage = 1;
			PaginationInfo<List<Articles.ModuleArticleForClientSummaryList>> paginationInfo = null;

			if (isPreview)	// 读预览数据。
			{
				string json = templateConfiguration.GetContentPreviewData(module.ModuleType, SpecialArticleContentViewType.List.ToString(), contentTemplate.ViewType);
				if (!string.IsNullOrEmpty(json)) paginationInfo = Serialization.DeserializeJson<PaginationInfo<List<Articles.ModuleArticleForClientSummaryList>>>(json);
			}
			else
			{
				HttpContext context = HttpContext.Current;
				currentPage = IntExtensions.ParseOrDefault(context.Request.QueryString[GenericUrlManager.Special_Query_ModulePageField], 1);
			}

			if(paginationInfo == null) paginationInfo = Articles.GetPaginationSummaryList(null, int.Parse(module.RefModuleId), null, con.ListPageSize.GetValueOrDefault(20), currentPage);
			if (paginationInfo == null) return content;

			content = contentTemplate.GetTemplate(con.ContentParameters);
			Regex reg = new Regex("<headerTemplate>((?:.|\n|\r)*?)</headerTemplate>", RegexOptions.IgnoreCase);
			content = reg.Replace(content, "$1");
			content = content.Replace("{ListTitle}", module.ModuleName);
			content = content.Replace("{CssClass}", string.IsNullOrEmpty(con.ListCssClass) ? "" : @" class=""" + con.ListCssClass + @"""");
			content = content.Replace("{AttachedClass}", string.IsNullOrEmpty(con.ListCssClass) ? "" : @" " + con.ListCssClass);
			content = content.Replace("{ImageWidthStyle}", con.ListImageWidth.HasValue ? "width:" + con.ListImageWidth.ToString() + "px;" : "");
			content = content.Replace("{ImageHeightStyle}", con.ListImageHeight.HasValue ? "height:" + con.ListImageHeight.ToString() + "px;" : "");
			content = content.Replace("{ModuleIndex}", page.ModuleCounter.ToString());

			// 默认图片Url。
			string defaultImageUrl = string.IsNullOrEmpty(contentTemplate.DefaultImageUrl) ? templateConfiguration.DefaultImageUrl : contentTemplate.DefaultImageUrl;
			if (!string.IsNullOrEmpty(defaultImageUrl))
			{
				defaultImageUrl = defaultImageUrl.Replace(SpecialPages.TemplateConst_TemplateRootUrl, SpecialPages.TemplateRootUrl);
				defaultImageUrl = defaultImageUrl.Replace(SpecialPages.TemplateConst_TemplateFolder, pageInfo.TemplateRootUrl);
			}

			Regex reg2 = new Regex("<itemTemplate>((?:.|\n|\r)*?)</itemTemplate>", RegexOptions.IgnoreCase);
			Match match2 = reg2.Match(content);
			if (match2 != Match.Empty)
			{
				string items = "";
				string itemTemplate = match2.Groups[1].Value;
				string imageStyle = "";
				if (con.ListImageWidth.HasValue || con.ListImageHeight.HasValue) imageStyle = @" style=""" + (con.ListImageWidth.HasValue ? "width:" + con.ListImageWidth + "px;" : "") + (con.ListImageHeight.HasValue ? "height:" + con.ListImageHeight + "px;" : "") + @"""";
				foreach (var data in paginationInfo.Records)
				{
					string item = itemTemplate.Replace("{ItemUrl}", BuildContentUrl(specialInfo.SpecialCode, contentPageCode, module.ModuleCode, data.ArticleId, null));
					item = item.Replace("{ItemTarget}", con.ListItemTarget);
					item = item.Replace("{ItemTargetAttribute}", string.IsNullOrEmpty(con.ListItemTarget) ? "" : @" target=""" + con.ListItemTarget + @"""");
					item = item.Replace("{ItemImage}", string.IsNullOrEmpty(data.ImageType) && string.IsNullOrEmpty(defaultImageUrl) ? "" : string.Format(@"<img src=""{0}"" alt=""{1}""{2} />", GenericUrlManager.GetInfoResourceThumbnailGeneratorUrl(data.ImageType, null, null, defaultImageUrl), data.Title, imageStyle));
					item = item.Replace("{ItemSourceTitle}", data.Title);
					item = item.Replace("{ItemTitle}", StringExtensions.CutString(data.Title, con.ListTitleCapacity, "..."));
					item = item.Replace("{ItemReleaseDate}", data.ReleaseDate.ToShortDateString());
					item = item.Replace("{ItemEncodeSourceTitle}", HttpUtility.HtmlEncode(data.Title));
					item = item.Replace("{ItemImageUrl}", GenericUrlManager.GetInfoResourceThumbnailGeneratorUrl(data.ImageType, null));
					item = item.Replace("{ItemSummary}", data.Summary);

					items += item;
				}

				content = reg2.Replace(content, items);
			}

			content = content.Replace("{Pager}", GenericHelper.BuildSpecialListPager(paginationInfo, GenericUrlManager.BuildSpecialModuleListUrlPattern(specialInfo.SpecialCode, contentPageCode, module.ModuleCode)));

			if (contentTemplate.HeadIncludeFiles != null)	// 头部包含文件
			{
				foreach (var file in contentTemplate.HeadIncludeFiles)
					page.RegisterHeadIncludeFile(file.Key, file.Value);
			}
			
			if (isPreview)		// 加载预览样式
			{
				page.TemplateCssUrls = templateConfiguration.TemplateCssUrls;
				if (page.TemplateCssUrls != null)
				{
					for (int i = 0; i < page.TemplateCssUrls.Length; i++)
					{
						var url = page.TemplateCssUrls[i];
						if (!string.IsNullOrEmpty(url))
							page.TemplateCssUrls[i] = url.Replace(SpecialPages.TemplateConst_TemplateRootUrl, SpecialPages.TemplateRootUrl);
					}
				}
			}

			// 替换其它模板变量
			content = GenericHelper.ReplaceSpecialTemplateParameters(content, contentTemplate.OtherParameters, con.ListParameters);

			// 当前位置
			page.Locations.Add(new NavigationItem() { Text = module.ModuleName, Url = BuildListUrl(specialInfo.SpecialCode, contentPageCode, module.ModuleCode, null) });
			page.Title += "_" + module.ModuleName + Page.TitleSectionSeparator + string.Format(Res.TitlePaging, currentPage) + Hxsoft.AppV1.Page.TitleSuffix;	// 更新页面标题
			page.MetaKeywords += (string.IsNullOrEmpty(page.MetaKeywords) ? "" : ",") + module.ModuleName;

			return content;
		}

		public string ParseCommentList(SpecialTemplateConfiguration templateConfiguration, SpecialModule module, Special specialInfo, SpecialPage pageInfo, ref SpecialPageInfo page, bool isPreview)
		{
			string content = "";
			if (module == null) return content;

			SpecialArticleContentConfiguration con = module.ContentConfigurationObject as SpecialArticleContentConfiguration;
			if (con == null) con = new SpecialArticleContentConfiguration();
			if (string.IsNullOrEmpty(con.ListItemTarget)) con.ListItemTarget = "_blank";
			SpecialContentTemplate contentTemplate = templateConfiguration.GetContentTemplate(module.ModuleType, SpecialArticleContentViewType.CommentList.ToString(), con.ContentType);
			if (contentTemplate == null) return content;

			int infoId = 0;
			Article article = null;

			if (isPreview)
			{
				string json = templateConfiguration.GetContentPreviewData(module.ModuleType, SpecialArticleContentViewType.CommentList.ToString(), contentTemplate.ViewType);
				if (!string.IsNullOrEmpty(json)) article = Serialization.DeserializeJson<Article>(json);
			}
			else
			{
				HttpContext context = HttpContext.Current;
				infoId = int.Parse(context.Request.QueryString[GenericUrlManager.Special_Query_ModuleInfoIdField]);
			}

			if (article == null) article = Articles.Read(infoId);
			if (article == null) return content;

			string contentPageCode = con.ContentPage;
			int currentPage = 1;
			PaginationInfo<List<Comments.CommentForClientList>> paginationInfo = null;

			content = contentTemplate.GetTemplate(con.ContentParameters);
			content = CommentSpecialModule.ParseCommentForm(page, content, Articles.INFO_TYPE, article.ArticleId.ToString(), page.ModuleCounter, false);

			if (isPreview)	// 读预览数据。
			{
				string json = templateConfiguration.GetContentPreviewData(module.ModuleType, SpecialArticleContentViewType.CommentList.ToString(), contentTemplate.ViewType);
				if (!string.IsNullOrEmpty(json)) paginationInfo = Serialization.DeserializeJson<PaginationInfo<List<Comments.CommentForClientList>>>(json);
			}
			else
			{
				HttpContext context = HttpContext.Current;
				currentPage = IntExtensions.ParseOrDefault(context.Request.QueryString[GenericUrlManager.Special_Query_ModulePageField], 1);
			}

			if (paginationInfo == null) paginationInfo = Comments.GetPaginationList(specialInfo.SiteId, Articles.INFO_TYPE, article.ArticleId.ToString(), con.ListPageSize.GetValueOrDefault(20), currentPage);
			if (paginationInfo == null) return content;

			content = CommentSpecialModule.ParseCommentList(page, content, paginationInfo.Records, true, true, null, "相关评论", BuildContentCommentUrl(specialInfo.SpecialCode, contentPageCode, module.ModuleCode, infoId, null), null, null);

			content = content.Replace("{Pager}", GenericHelper.BuildSpecialListPager(paginationInfo, GenericUrlManager.BuildSpecialModuleContentCommentUrlPattern(specialInfo.SpecialCode, contentPageCode, module.ModuleCode, infoId.ToString())));

			if (contentTemplate.HeadIncludeFiles != null)	// 头部包含文件
			{
				foreach (var file in contentTemplate.HeadIncludeFiles)
					page.RegisterHeadIncludeFile(file.Key, file.Value);
			}

			if (isPreview)		// 加载预览样式
			{
				page.TemplateCssUrls = templateConfiguration.TemplateCssUrls;
				if (page.TemplateCssUrls != null)
				{
					for (int i = 0; i < page.TemplateCssUrls.Length; i++)
					{
						var url = page.TemplateCssUrls[i];
						if (!string.IsNullOrEmpty(url))
							page.TemplateCssUrls[i] = url.Replace(SpecialPages.TemplateConst_TemplateRootUrl, SpecialPages.TemplateRootUrl);
					}
				}
			}

			// 当前位置
			page.Locations.Add(new NavigationItem() { Text = module.ModuleName, Url = BuildListUrl(specialInfo.SpecialCode, contentPageCode, module.ModuleCode, null) });
			page.Locations.Add(new NavigationItem() { Text = article.Title, Url = BuildContentUrl(specialInfo.SpecialCode, contentPageCode, module.ModuleCode, article.ArticleId, null) });
			page.Locations.Add(new NavigationItem() { Text = "相关评论", Url = BuildContentCommentUrl(specialInfo.SpecialCode, contentPageCode, module.ModuleCode, infoId, null) });
			page.Title = article.Title + Hxsoft.AppV1.Page.TitleSuffix;	// 更新页面标题
			page.MetaKeywords = article.Title + (string.IsNullOrEmpty(article.Keywords) ? "" : "," + article.Keywords);
			page.MetaDescription = article.Summary;

			return content;
		}

		#region ISpecialModuleParser 成员

		string ISpecialModuleParser.ParsePreviewPart(SpecialTemplateConfiguration templateConfiguration, SpecialModule module, SpecialPageConfiguration configuration, Special specialInfo, SpecialPage pageInfo, ref SpecialPageInfo page)
		{
			return ParsePart(templateConfiguration, module, configuration, specialInfo, pageInfo, ref page, true);
		}

		string ISpecialModuleParser.ParsePreviewContent(SpecialTemplateConfiguration templateConfiguration, SpecialModule module, Special specialInfo, SpecialPage pageInfo, ref SpecialPageInfo page, string moduleView)
		{
			if (moduleView == "Content")
				return ParseContent(templateConfiguration, module, specialInfo, pageInfo, ref page, true);
			else
				return ParseList(templateConfiguration, module, specialInfo, pageInfo, ref page, true);
		}


		string ISpecialModuleParser.ParsePart(SpecialTemplateConfiguration templateConfiguration, SpecialModule module, SpecialPageConfiguration configuration, Special specialInfo, SpecialPage pageInfo, ref SpecialPageInfo page)
		{
			return ParsePart(templateConfiguration, module, configuration, specialInfo, pageInfo, ref page, false);
		}

		string ISpecialModuleParser.ParseContent(SpecialTemplateConfiguration templateConfiguration, SpecialModule module, Special specialInfo, SpecialPage pageInfo, ref SpecialPageInfo page)
		{
			HttpContext context = HttpContext.Current;
			string view = context.Request.QueryString[GenericUrlManager.Special_Query_ModuleViewField];
			if (view == SpecialArticleContentViewType.Content.ToString())
				return ParseContent(templateConfiguration, module, specialInfo, pageInfo, ref page, false);
			else if (view == SpecialArticleContentViewType.CommentList.ToString())
				return ParseCommentList(templateConfiguration, module, specialInfo, pageInfo, ref page, false);
			else
				return ParseList(templateConfiguration, module, specialInfo, pageInfo, ref page, false);
		}

		#endregion
	}
}